/**
 * 
 */
package com.holystone.orca.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.holystone.orca.base.service.impl.BaseServiceImpl;
import com.holystone.orca.common.PageData;
import com.holystone.orca.sys.mybatis.dao.SysPrivilegeMapper;
import com.holystone.orca.sys.mybatis.dao.SysPrivilegeRoleMapper;
import com.holystone.orca.sys.mybatis.model.SysPrivilege;
import com.holystone.orca.sys.mybatis.model.SysPrivilegeRole;
import com.holystone.orca.sys.service.ISysPrivilegeService;


@Service(value="privilegeService")
@Transactional
public class SysPrivilegeServiceImpl extends BaseServiceImpl<SysPrivilege> implements ISysPrivilegeService {
	@Autowired
	private SysPrivilegeMapper sysPrivilegeMapper;
	@Autowired
	private SysPrivilegeRoleMapper sysPrivilegeRoleMapper;

	@Override
	public List<SysPrivilege> selectAllPrivilege() {
		List<SysPrivilege> privilegeList=sysPrivilegeMapper.selectAllPrivilege();
		return privilegeList;
	}

	@Override
	public boolean selectPrivilegeByRoleId(int roleId,int priId) {
		List<SysPrivilegeRole> privilegeRoleList=sysPrivilegeRoleMapper.selectPrivilegeByRoleId(roleId);
		for(int i=0;i<privilegeRoleList.size();i++){
			if(privilegeRoleList.get(i).getPriId()==priId){
				return true;
			}
		}
		return false;
	}

	@Override
	public List<SysPrivilege> selectPrivilegeByPriId(List<Integer> priIdList) throws Exception {
		List<SysPrivilege> list = sysPrivilegeMapper.selectPrivilegeByPriId(priIdList);
		
		return list;
	}

	@Override
	public PageData pagingSysPrivilegeList(int iPageNo, int iPageSize,
			String orderField, String priId,boolean msg) throws Exception {
		 	PageHelper.startPage(iPageNo, iPageSize);
		 	PageInfo<SysPrivilege> pageInfo = null;
		 	if(msg){
		 		pageInfo = new PageInfo<SysPrivilege>(sysPrivilegeMapper.selectPrivilegeByPriIdOrPriPid(Integer.parseInt(priId)));
		 	}else{
		 		pageInfo = new PageInfo<SysPrivilege>(sysPrivilegeMapper.selectParentPrivilegeByPriId(Integer.parseInt(priId)));
		 	}
			PageData pageData =  new PageData();
			pageData.setPageList(pageInfo.getList());
			pageData.setTotalCount(pageInfo.getTotal());
			pageData.setPageSize(pageInfo.getPageSize());
			pageData.setPageNo(pageInfo.getPageNum());
			pageData.setTotalPage(pageInfo.getPages());
			return pageData;
	}

	@Override
	public List<SysPrivilege> selectPrivilegePriName(List<Integer> list) throws Exception {
		List<SysPrivilege> sysPrivilegeList = sysPrivilegeMapper.selectPrivilegePriName(list);
		return sysPrivilegeList;
	}

	@Override
	public List<SysPrivilege> sysPrivilegeList(String priId) throws Exception {
		List<SysPrivilege> sysPrivilegeList = sysPrivilegeMapper.selectPrivilegeByPriIdOrPriPid(Integer.parseInt(priId));
		return sysPrivilegeList;
	}

	@Override
	public boolean addBroPrivilege(SysPrivilege sysPrivilege) throws Exception {
		int result=sysPrivilegeMapper.addBroPrivilege(sysPrivilege);
		if(result==1){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 修改时权限时更新url的可操作权限
	 * @author: 李海宁
	 * @date: 2016年10月21日 下午4:37:37
	 * @param url 访问页面的URL
	 * @param actionPri 页面的可操作权限
	 * @param priId 要修改的权限节点
	 * @return
	 */
	private boolean updateUrlActionPri(String url,String actionPri,Integer priId){
		
		return false;
	}
	/**
	 * 新增时权限时更新url的可操作权限
	 * @author: 李海宁
	 * @date: 2016年10月21日 下午4:37:37
	 * @param url 访问页面的URL
	 * @param actionPri 页面的可操作权限
	 * @param priId 要修改的权限节点
	 * @return
	 */
	private boolean updateUrlActionPri(String url,String actionPri) throws Exception{
		Example emp = new Example(SysPrivilege.class);
		Criteria cri = emp.createCriteria();
		cri.andEqualTo("progCode", url);
		List<SysPrivilege> list = selectByExample(emp);
		if(list!=null&&list.size()>0){
			for(SysPrivilege sp : list){
				sp.setActionPri(actionPri);
				updateAll(sp);
				//sysPrivilegeRoleMapper.editActionPri(sp.getPriId(),sysPrivilege.getActionPri());
			}
		}
		return true;
	}

	@Override
	public SysPrivilege selectParentPrivilegeByPriId(int priId)
			throws Exception {
		List<SysPrivilege> sysPrivilege = sysPrivilegeMapper.selectParentPrivilegeByPriId(priId);
		return sysPrivilege.get(0);
	}

	@Override
	public SysPrivilege selectPriByPriId(int priId) throws Exception {
		List<SysPrivilege> sysPrivilege = sysPrivilegeMapper.selectPriByPriId(priId);
		if(sysPrivilege.size() == 0){
			return null;
		}else{
			return sysPrivilege.get(0);
		}
	}
	
	@Override
	public List<SysPrivilege> selectPriviByPriId(Integer priId) throws Exception {
		List<SysPrivilege> sysPrivilege = sysPrivilegeMapper.selectPriByPriId(priId);
		return sysPrivilege;
	}

	@Override
	public boolean addChildPrivilege(SysPrivilege sysPrivilege)
			throws Exception {
		int result=sysPrivilegeMapper.addChildPrivilege(sysPrivilege);
		if(result==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean editPrivilege(SysPrivilege sysPrivilege,String oldActionPri) throws Exception {
		int result=sysPrivilegeMapper.editPrivilege(sysPrivilege);
		if(oldActionPri!=null&&oldActionPri.length()>0){
			List<String> roleActionPri = new ArrayList<String>();
			String newActionPri = sysPrivilege.getActionPri();
			for(String oldPri:oldActionPri.split(",")){
				if(newActionPri.contains(oldPri)){
					roleActionPri.add(oldPri);
				}
			}
			List<SysPrivilege> sysPrivilegeList = selectPriviByUrl(sysPrivilege.getProgCode());
			String spids = "";
			for(SysPrivilege sp : sysPrivilegeList){
				spids+=sp.getPriId()+",";
			}
			if(roleActionPri.size()>0){
				sysPrivilegeRoleMapper.editActionPri(spids.substring(0, spids.length()-1),StringUtils.join(roleActionPri, ","));
			}
		}
		sysPrivilegeMapper.editUrlActionPri(sysPrivilege.getProgCode(),sysPrivilege.getActionPri());
		if(result==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean delPrivilege(int priId) throws Exception {
		int result=sysPrivilegeMapper.delPrivilege(priId);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean delSelectPrivilege(List<Integer> list) throws Exception {
		int result=sysPrivilegeMapper.delSelectPrivilege(list);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public SysPrivilege selectIsParentPri(Integer priId) throws Exception {
		SysPrivilege sysPrivilege = sysPrivilegeMapper.selectIsParentPri(priId);
		return sysPrivilege;
	}

	@Override
	public List<SysPrivilege> selectAllOpenPrivilege() {
		List<SysPrivilege> privilegeList=sysPrivilegeMapper.selectAllOpenPrivilege();
		return privilegeList;
	}

	@Override
	public List<SysPrivilege> selectPriviByUrl(String url) throws Exception {
		List<SysPrivilege> privilegeList=sysPrivilegeMapper.selectPriviByUrl(url);
		return privilegeList;
	}

	
}
